//
//  WelcomeViewController.swift
//  weibopush
//
//  Created by 龚欣 on 15/6/26.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

class WelcomeViewController: UIViewController {
   
    override func loadView() {
        view = UIView()
        
        view.addSubview(backgroundView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)

        backgroundView.ff_Fill(view, insets: UIEdgeInsetsMake(0, 0, 0, 0))
        
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(85, 85), offset: CGPoint(x: 0, y: -UIScreen.mainScreen().bounds.height * 0.5))
        iconViewBottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)

        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 10))
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewBottomConstraint?.constant = -UIScreen.mainScreen().bounds.height * 0.7
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.view.layoutIfNeeded()
            }) { _ in
                UIView.animateWithDuration(0.5, animations: {
                    self.messageLabel.alpha = 1.0
                    }, completion: { _ in
                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName("SwitchRootViewControllerNotification", object: true)
                })
        }
    }
    
   
    var iconViewBottomConstraint : NSLayoutConstraint?
   
    lazy var backgroundView : UIImageView = {
        let backView = UIImageView(image: UIImage(named: "ad_background"))
        return backView
        }()
    
    lazy var iconView : UIImageView = {
        let iconV = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconV.layer.masksToBounds = true
        iconV.layer.cornerRadius = (UIImage(named: "avatar_default_big")?.size.width)! * 0.5
        return iconV
        }()
    
    lazy var messageLabel : UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.sizeToFit()
        label.alpha = 0.0
        return label
        }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        iconView.sd_setImageWithURL(NSURL(string: (sharedAccount?.avatar_large)!))
        
    }

}
